package com.mall.ware.listener;

import com.mall.common.dto.OrderDTO;
import com.mall.common.dto.OrderNoteDto;
import com.mall.common.dto.WareOrderTaskDetailDTO;
import com.mall.common.enume.OrderStatusEnum;
import com.mall.common.utils.Result;
import com.mall.ware.entity.WareOrderTaskDetailEntity;
import com.mall.ware.entity.WareOrderTaskEntity;
import com.mall.ware.feign.OrderFeignClient;
import com.mall.ware.service.WareOrderTaskDetailService;
import com.mall.ware.service.WareOrderTaskService;
import com.mall.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Objects;


/**
 * @author: 谢君臣
 * @Date: 2022/8/11-16:39
 * @version: 1.0
 * @function:
 */
@Slf4j
@RabbitListener(queues = "stock.reduce.queue")
@Component
public class ReduceStockListener {

    @Resource
    private WareOrderTaskService orderTaskService;

    @RabbitHandler
    public void releaseLockStock(OrderNoteDto orderDTO, Message message, Channel channel) throws IOException {
        try {
            log.info("订单支付成功,减库存消息");
            orderTaskService.reduceStock(orderDTO.getOrderSn());
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (RuntimeException e) {
            log.info(e.getMessage());
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }

}
